﻿using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;

namespace Selenium
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在启动...");
            var options = new EdgeOptions();
            using IWebDriver driver = InitialWebDriver(options);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            try
            {
                ConsoleKeyInfo keyInfo;
                do
                {
                    driver.Navigate().GoToUrl("https://www.baidu.com");
                    Console.WriteLine("请输入搜索关键字：");
                    var queryString = Console.ReadLine();
                    driver.FindElement(By.Id("kw")).SendKeys(queryString);
                    //模拟点击
                    driver.FindElement(By.Id("su")).Click();
                    Console.WriteLine("按“Enter”键继续，其他任意键退出");
                    keyInfo = Console.ReadKey();
                } while (keyInfo.Key == ConsoleKey.Enter);
                driver.Quit();
                //((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
                //var text = wait.Until(ExpectedConditions.ElementExists(By.Id("kw")));
                //driver.FindElement(By.Name("q")).SendKeys("cheese" + Keys.Enter);
                //IWebElement firstResult = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("h3>div")));
                //Console.WriteLine(firstResult.GetAttribute("textContent"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        /// 初始化Edge浏览器
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IWebDriver InitialWebDriver(EdgeOptions options)
        {
            //EdgeDriverService service = EdgeDriverService.CreateDefaultService(@".\", "msedgedriver.exe");
            //var edgeDriver = new EdgeDriver(service, options);
            var edgeDriver = new EdgeDriver(options);
            return edgeDriver;
        }

        /// <summary>
        /// 初始化Chrome浏览器
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IWebDriver InitialWebDriver(ChromeOptions options)
        {
            var chromeDriver = new ChromeDriver(options);
            return chromeDriver;
        }

        /// <summary>
        /// 初始化Firefox浏览器
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IWebDriver InitialWebDriver(FirefoxOptions options)
        {
            options.AddArguments(new string[] { "--headless", "--incognito", "--start-maximized" });//启用无头模式
            var firefoxDriver = new FirefoxDriver(options);
            return firefoxDriver;
        }
    }
}
